<template>
  <div>
    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="pageInfo.query">
            <el-button
              slot="append"
              @click="searchInput"
              icon="el-icon-search"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-row>
        <el-table :data="orderList" border stripe>
          <el-table-column type="index"></el-table-column>
          <el-table-column
            label="订单编号"
            prop="order_number"
          ></el-table-column>
          <el-table-column
            label="订单价格"
            prop="order_price"
          ></el-table-column>
          <el-table-column label="是否付款" prop="pay_status">
            <template v-slot="scope">
              <el-tag type="success" v-if="scope.row.pay_status === '1'"
                >已付款</el-tag
              >
              <el-tag type="danger" v-else>未付款</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="是否发货" prop="is_send"></el-table-column>
          <el-table-column label="下单时间" prop="create_time">
            <template v-slot="scope">
              {{ scope.row.create_time | dateFormat }}
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template v-slot="scope">
              <el-tooltip
                class="item"
                effect="dark"
                content="修改状态"
                placement="top"
                :enterable="false"
              >
                <el-button
                  @click="isChangeOrder(scope.row)"
                  type="primary"
                  icon="el-icon-edit"
                  circle
                ></el-button>
              </el-tooltip>
              <el-tooltip
                class="item"
                effect="dark"
                content="物流状态"
                placement="top"
                :enterable="false"
              >
                <el-button
                  @click="showOrderInfo(scope.row)"
                  type="success"
                  icon="el-icon-location"
                  circle
                ></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <el-row>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageInfo.pagenum"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="pageInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </el-row>
    </el-card>
    <!-- 修改的弹出框 懒得写分离组件了-->
    <el-dialog
      title="修改地址"
      :visible.sync="dialogVisible"
      width="800px"
      @close="addressDialogClosed"
    >
      <el-form
        :model="addressForm"
        :rules="addressFormRules"
        ref="addressFormRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            :options="cityData"
            v-model="addressForm.address1"
            :props="{ expandTrigger: 'hover' }"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addAddress"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- //显示物流 -->
    <el-dialog
      title="物流进度"
      :visible.sync="progressDialogVisible"
      width="800px"
    >
      <el-timeline :reverse="true">
        <el-timeline-item
          v-for="(activity, index) in addressList"
          :key="index"
          :timestamp="activity.time"
        >
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import { getOrdersDataApi, getAddressDataApi } from '@/Utils/Api/gteOrdersInfoApi'
import { addressFormRule } from '@/common/mixin'
import cityData from './citydata'

export default {
  name: 'OrdersView',
  mixins: [addressFormRule],
  data () {
    return {
      pageInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      orderList: [],
      total: 0,
      dialogVisible: false,
      progressDialogVisible: false,
      addressForm: {
        address1: [],
        address2: ''
      },
      cityData,
      addressList: []
    }
  },
  components: {},
  created () {
    this.getOrdersData()
  },
  mounted () {},
  methods: {
    async getOrdersData () {
      const { data } = await getOrdersDataApi(this.pageInfo)
      console.log(data)
      if (data.meta.status !== 200) {
        return this.$message({
          type: 'error',
          message: data.meta.msg
        })
      }
      this.orderList = data.data.goods
      this.total = data.data.tota
    },
    searchInput () {
      this.getOrdersData()
    },
    isChangeOrder () {
      this.dialogVisible = true
    },
    addAddress () {
      this.dialogVisible = false
      return this.$message({
        type: 'error',
        message: '这个功能偷懒没写哦'
      })
    },
    // 展示物流进度
    async showOrderInfo () {
      const { data } = await getAddressDataApi()
      if (data.meta.status !== 200) {
        return this.$message({
          type: 'error',
          message: data.meta.msg
        })
      }
      this.addressList = data.data
      this.$message({
        type: 'success',
        message: data.meta.msg
      })
      this.progressDialogVisible = true
    },
    // 分页
    handleSizeChange (newSize) {
      this.pageInfo.pagesize = newSize
      this.getOrdersData()
    },
    handleCurrentChange (newNum) {
      this.pageInfo.pagenum = newNum
      this.getOrdersData()
    },
    // 清空添加表单
    addressDialogClosed () {
      this.$refs.addressFormRef.resetFields()
    }
  },
  computed: {},
  watch: {}
}
</script>

<style lang="less" scoped>
.el-table {
  margin-top: 15px;
}
.cascader {
  width: 100%;
}
</style>
